package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.dto.OrderDto;
import com.itheima.reggie.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.jgss.GSSHeader;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     *
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        //获取当前登录的用户
        Long userId = (Long) session.getAttribute("user");
        //2。把用户的信息设置到orders里面
        orders.setUserId(userId);
        //3 交给orderService
        orderService.addOrder(orders);
        return R.success("下单成功");


    }

    /*
    管理端查询订单列表
     */
    @GetMapping("/page")
    public R<Page<Orders>> save(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize, String number,String benTime,String endTime){
        Page<Orders> pageResult = orderService.findByPage(page,pageSize,number,benTime,endTime);
        return R.success(pageResult);
    }

    /*
    移动端订单查询
     */
    @GetMapping("/userPage")
    public R<Page<OrderDto>> save(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        Page<OrderDto> pageResult = orderService.findByPageUserid(page,pageSize,userId);
        return R.success(pageResult);
    }
}
